﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Timers;
using RxjhServer.DbClss;

namespace RxjhServer
{
    public class EventClass : IDisposable
    {
        private System.Timers.Timer 时间1;

        private System.Timers.Timer 时间2;

        private System.Timers.Timer 时间3;

        private System.Timers.Timer 时间4;

        private System.Timers.Timer 时间5;

        private readonly DateTime kssj;

        private DateTime kssjgj;

        private int kssjint;

        private int 势力战sj;

        //private string jlsqlzj = string.Empty;

        //private int jlsqlzj;
        public void 势力战第一滴血获得物品(Players play)
        {
            try
            {
                string[] array = World.势力战奖励个人物品.Split(new char[]
                {
                            ','
                });
                if (array.Length > 0)
                {
                    if (int.Parse(array[0]) != 0)
                    {
                        int num = -1;
                        try
                        {
                            num = int.Parse(array[1]);
                        }
                        catch
                        {
                        }
                        ItmeClass itmeClass = new ItmeClass();
                        if (num == -1)
                        {
                            itmeClass = ItmeClass.GetItme(array[1]);
                        }
                        else
                        {
                            itmeClass = ItmeClass.GetItmeID(num);
                        }
                        if (itmeClass != null)
                        {
                            int num2 = 1;
                            if (array.Length >= 3)
                            {
                                num2 = int.Parse(array[2]);
                            }
                            if (num2 < 1)
                            {
                                num2 = 1;
                            }
                            for (int i = 1; i <= num2; i++)
                            {
                                int num3 = play.得到包裹空位(play);
                                if (num3 == -1)
                                {
                                    break;
                                }
                                play.增加物品带属性(itmeClass.FLD_PID, num3, num2, int.Parse(array[3]), int.Parse(array[4]), int.Parse(array[5]), int.Parse(array[6]), int.Parse(array[7]), int.Parse(array[8]));
                            }
                            play.系统提示("恭喜您获得 " + itmeClass.ItmeNAME + " 第一滴血奖励", 10, "势力战奖励");
                            //World.conn.发送(string.Concat(new string[]
                            //{
                            //            "全服公告|10|势力战|势力战第一滴血获得者是[",
                            //            play.UserName,
                            //            "]恭喜其获得物品[",
                            //            itmeClass.ItmeNAME,
                            //            "]"
                            //}));

                            World.发送公告("势力战第一滴血获得者是["+play.UserName+"]恭喜其获得物品["+itmeClass.ItmeNAME+"]");


                            logo.获奖人数Log("势力战第一滴血获得者是[" + play.UserName + "]");
                        }
                    }
                }
            }
            catch (Exception arg)
            {
                //Dispose();
                Form1.WriteLine(1, "势力战第一滴血获得物品 出错：" + arg);
            }
        }
        public EventClass(int 几转)
        {
            try
            {
                DBA.ExeSqlCommand("DELETE FROM EventTop");
                World.势力战第一滴血 = 0;
                World.EventTop.Clear();
                this.kssj = DateTime.Now.AddMinutes(10.0);
                World.势力战进程 = 1;
                World.势力战正分数 = 0;
                World.势力战邪分数 = 0;
                World.几转势力战 = 几转;
                this.时间1 = new System.Timers.Timer(60000.0);
                this.时间1.Elapsed += new ElapsedEventHandler(this.时间结束事件1);
                this.时间1.Enabled = true;
                this.时间1.AutoReset = true;
                this.时间结束事件1(null, null);
              //  World.conn.发送("势力战进程|" + World.势力战进程);
            }
            catch (Exception arg)
            {
                Form1.WriteLine(1, "势力战 EventClass 出错：" + arg);
            }
        }

        public void 时间结束事件1(object source, ElapsedEventArgs e)
         {
            try
            {
                int num = (int)this.kssj.Subtract(DateTime.Now).TotalSeconds;
                if (num <= 0)
                {
                    World.势力战进程 = 2;
                    World.势力战正分数 = 0;
                    World.势力战邪分数 = 0;
                   // World.conn.发送("势力战进程|" + World.势力战进程);
                    num = 0;
                }
                this.kssjint = num;
                if (World.allConnectedChars.Count > 0)
                {
                    foreach (Players current in World.allConnectedChars.Values)
                    {
                        if (current.人物坐标_地图 == 801)
                        {
                            current.发送势力战快开始消息(this.kssjint);
                        }
                    }
                }
                if (World.几转势力战==3)
                {
                    //World.conn.发送(string.Concat(new object[]
                    //{
                       

                    //    "发送公告|",
                    //    0,
                    //    "|离2/3转仙魔战开始还剩",
                    //    this.kssjint / 60,
                    //    "分钟,请从韦大宝处进入银币报名！"
                    //})) ;

                    World.发送公告("离2/3转仙魔战开始还剩"+this.kssjint / 60+"分钟,命令[@参加势力战]");
                }
                if (World.几转势力战 == 4)
                    {
                    //    World.conn.发送(string.Concat(new object[]
                    //    {
                    //        "发送公告|",
                    //        0,
                    //        "|离4转仙魔战战开始还剩",
                    //        this.kssjint / 60,
                    //        "分钟。请从韦大宝处进入银币报名！"
                    //    }));

                    World.发送公告("离4转仙魔战开始还剩" + this.kssjint / 60 + "分钟,命令[@参加势力战]");
            }
                if (this.kssjint <= 0)
                {
                    bool flag = true;
                    foreach (Players current2 in World.allConnectedChars.Values)
                    {
                        if (current2.人物坐标_地图 == 801)
                        {
                            flag = false;
                        }
                    }
                    if (flag)
                    {
                        this.Dispose();
                    }
                    else
                    {
                        this.时间1.Enabled = false;
                        this.时间1.Close();
                        this.时间1.Dispose();
                        this.时间1 = null;
                        World.势力战进程 = 3;
                    //    World.conn.发送("势力战进程|" + World.势力战进程);
                        this.kssjgj = DateTime.Now.AddMinutes((double)World.势力战时长);
                        this.时间2 = new System.Timers.Timer(10000.0);
                        this.时间2.Elapsed += new ElapsedEventHandler(this.时间结束事件2);
                        this.时间2.Enabled = true;
                        this.时间2.AutoReset = true;
                    }
                }
            }
            catch (Exception arg)
            {
                Form1.WriteLine(1, "势力战 时间结束事件1 出错：" + arg);
            }
        }

        public void 时间结束事件2(object source, ElapsedEventArgs e)
        {
            try
            {
                int num = (int)this.kssjgj.Subtract(DateTime.Now).TotalSeconds;
                World.势力战时间 = num;
                foreach (Players current in World.allConnectedChars.Values)
                {
                    if (current.人物坐标_地图 == 801)
                    {
                        current.发送势力战消息();
                    }
                }
                if (num <= 0)
                {
                    this.时间2.Enabled = false;
                    this.时间2.Close();
                    this.时间2.Dispose();
                    this.时间2 = null;
                    World.势力战进程 = 4;
                   // World.conn.发送("势力战进程|" + World.势力战进程);
                    this.时间3 = new System.Timers.Timer(10000.0);
                    this.时间3.Elapsed += new ElapsedEventHandler(this.时间结束事件3);
                    this.时间3.Enabled = true;
                    this.时间3.AutoReset = false;
                }
            }
            catch (Exception arg)
            {
                Form1.WriteLine(1, "势力战 时间结束事件2 出错：" + arg);
            }
        }

        public void 时间结束事件3(object source, ElapsedEventArgs e)
        {
            try
            {
              //  World.conn.发送("势力战进程|" + World.势力战进程);
                this.时间3.Enabled = false;
                this.时间3.Close();
                this.时间3.Dispose();
                this.时间3 = null;
                this.时间4 = new System.Timers.Timer(10000.0);
                this.时间4.Elapsed += new ElapsedEventHandler(this.时间结束事件4);
                this.时间4.Enabled = true;
                this.时间4.AutoReset = false;
                foreach (EventTopClass current in World.EventTop.Values)
                {
                    RxjhClass.保存势力战数据(current);
                }
                if (World.势力战正分数 > World.势力战邪分数)
                {
                    this.势力战sj = 1;
                    //this.jlsqlzj = "1";

                    World.发送公告("仙魔战结束[仙族]胜利。");
                    //World.conn.发送("发送公告|" + 0 + "|仙魔战结束[仙族]胜利。");
                }
                else if (World.势力战正分数 == World.势力战邪分数)
                {
                    this.势力战sj = 3;
                    //this.jlsqlzj = string.Empty;
                   // World.conn.发送("发送公告|" + 0 + "|仙魔战结束,双方平局。");
                    World.发送公告("仙魔战结束,双方平局。");
                }
                else
                {
                    this.势力战sj = 2;
                    //this.jlsqlzj = "2";
                   // World.conn.发送("发送公告|" + 0 + "|仙魔战结束[魔族]胜利。");


                    World.发送公告("仙魔战结束[魔族]胜利。");
                }
                foreach (Players current2 in World.allConnectedChars.Values)
                {
                    if (current2.人物坐标_地图 == 801)
                    {
                        current2.发送势力战结束消息(this.势力战sj);
                        if (current2.势力战临时正邪值 == this.势力战sj)
                        {
                            if (current2.势力战杀人数 >= 1)
                            {
                                this.jlwp(current2, true);
                            }
                        }
                        else if (current2.势力战杀人数 >= 1)
                        {
                            this.jlwp(current2, false);
                        }
                    }
                }
                try
                {
                    this.随机奖励();
                }
                catch (Exception arg)
                {
                    Form1.WriteLine(1, "势力战 随机奖励() 出错：" + arg);
                    Form1.WriteLine(100, "势力战 随机奖励()  出错：" + arg);
                }
                try
                {
                    this.奖励前五名();
                }
                catch (Exception arg2)
                {
                    Form1.WriteLine(1, "势力战 奖励前五名() 出错：" + arg2);
                    Form1.WriteLine(100, "势力战 奖励前五名()  出错：" + arg2);
                }
            }
            catch (Exception arg3)
            {
                Form1.WriteLine(1, "势力战 时间结束事件3 出错：" + arg3);
            }
            finally
            {
                World.EventTop.Clear();
            }
        }

        public void jlwp(Players Play, bool 胜败)
        {
            try
            {
                if (Play != null)
                {
                    int num2 = 18000000;
                    if (Play.追加状态列表.ContainsKey(1008000095))
                    {
                        追加状态类 追加状态类 = Play.追加状态列表[1008000095];
                        num2 += 追加状态类.FLD_sj;
                        追加状态类.时间结束事件();
                    }
                    追加状态类 追加状态类2 = new 追加状态类(Play, num2, 1008000095, 1);
                    Play.追加状态列表.Add(追加状态类2.FLD_PID, 追加状态类2);
                    Play.FLD_人物_追加_经验百分比 += 0.2;
                    Play.状态效果(BitConverter.GetBytes(1008000095), 1, num2);
                    Play.Player_WuXun += World.势力战基础奖励武勋;
                    Play.更新武功和状态();
                    Play.查百宝阁元宝数();
                    Play.检察积分数据(10, 1);
                    Play.保存元宝数据();
                    //Play.保存人物的数据();
                    Play.系统提示("恭喜您获得奖励" + World.势力战基础奖励武勋 + "武勋,赠品积分10个。", 10, "系統提示");
                    //byte[] bytes = BitConverter.GetBytes(9000120);
                    //if (!胜败)
                    //{
                    //    //bytes = BitConverter.GetBytes(9009150);
                    //}
                    //byte[] bytes2 = BitConverter.GetBytes(1);
                    //byte[] bytes3 = BitConverter.GetBytes(RxjhClass.GetDBItmeId());
                    //int num = Play.得到包裹空位(Play);
                    //if (num != -1)
                    //{
                    //    Play.增加物品(bytes3, bytes, num, bytes2, new byte[20]);
                    //    Play.系统提示("恭喜您获得势力战前50名奖励[1.2倍经验状态5小时]", 10, "奖励");
                    //}
                }
                Play.更新武功和状态();
            }
            catch
            {
            }
        }

        public void 随机奖励()
        {
            List<Players> list = new List<Players>();
            try
            {
                foreach (Players current in World.allConnectedChars.Values)
                {
                    if (current.人物坐标_地图 == 801 && current.势力战临时正邪值 == this.势力战sj)
                    {
                        list.Add(current);
                    }
                }
                if (list.Count > 0 && World.势力战随机奖励物品 != 0)
                {
                    int 势力战随机奖励物品 = World.势力战随机奖励物品;
                    int index = RNG.Next(0, list.Count - 1);
                    byte[] bytes = BitConverter.GetBytes(RxjhClass.GetDBItmeId());
                    int num = list[index].得到包裹空位(list[index]);
                    ItmeClass itmeClass;
                    if (num != -1 && World.Itme.TryGetValue(势力战随机奖励物品, out itmeClass))
                    {
                        list[index].增加物品(bytes, BitConverter.GetBytes(势力战随机奖励物品), num, BitConverter.GetBytes(1), new byte[20]);

                        World.发送公告("胜方玩家["+list[index].UserName+"]在仙魔战中获得"+  itmeClass.ItmeNAME+",奖励人物由系统随机抽选。");

                        //World.全局提示("系統提示", 10, string.Concat(new string[]
                        //{
                        //    "胜方玩家[",
                        //    list[index].UserName,
                        //    "]在仙魔战中获得",
                        //    itmeClass.ItmeNAME,
                        //    ",奖励人物由系统随机抽选。"
                        //}));
                    }
                }
                list.Clear();
                list = null;
            }
            catch (Exception arg)
            {
                list.Clear();
                list = null;
                Form1.WriteLine(1, "势力战 时间结束事件33 出错：" + arg);
            }
        }

        public void 奖励前五名()
        {
            try
            {
                DataTable dataTable = RxjhClass.得到势力战数据();
                if (dataTable != null)
                {
                    for (int i = 0; i < dataTable.Rows.Count; i++)
                    {
                        Players players = World.检查玩家name(dataTable.Rows[i]["人物名"].ToString());
                        if (players != null)
                        {
                            if (i >= 0 && i < 5)
                            {
                                if (World.势力战前五名奖励武勋 > 0)
                                {
                                    players.Player_WuXun += World.势力战前五名奖励武勋;
                                    players.查百宝阁元宝数();
                                    players.检察积分数据(50, 1);
                                    players.保存元宝数据();
                                    players.系统提示("恭喜您获得奖励" + World.势力战前五名奖励武勋 + "武勋,赠品元宝50个。", 10, "系統提示");
                                    //if (players != null)
                                    //{
                                    //	try
                                    //	{
                                    //		int num2 = 18000000;
                                    //		if (players.追加状态列表.ContainsKey(1008000095))
                                    //		{
                                    //			追加状态类 追加状态类 = players.追加状态列表[1008000095];
                                    //			num2 += 追加状态类.FLD_sj;
                                    //			追加状态类.时间结束事件();
                                    //		}
                                    //		追加状态类 追加状态类2 = new 追加状态类(players, num2, 1008000095, 1);
                                    //		players.追加状态列表.Add(追加状态类2.FLD_PID, 追加状态类2);
                                    //		players.FLD_人物_追加_经验百分比 += 1.1;
                                    //		players.状态效果(BitConverter.GetBytes(1008000095), 1, num2);
                                    //		players.更新武功和状态();
                                    //		players.保存人物的数据();
                                    //		players.系统提示("恭喜您获得势力战前50名奖励[1.2倍经验状态5小时]", 10, "奖励");
                                    //	}
                                    //	catch (Exception)
                                    //	{
                                    //	}
                                    //}
                                    players.更新武功和状态();
                                }
                            }
                            else if (i >= 5 && i < 10)
                            {
                                if (World.势力战五到十名奖励武勋 > 0)
                                {
                                    players.Player_WuXun += World.势力战五到十名奖励武勋;
                                    players.查百宝阁元宝数();
                                    players.检察积分数据(30, 1);
                                    players.保存元宝数据();
                                    players.系统提示("恭喜您获得奖励" + World.势力战五到十名奖励武勋 + "武勋,赠品元宝30个。", 10, "系統提示");
                                    //if (players != null)
                                    //{
                                    //	try
                                    //	{
                                    //		int num2 = 18000000;
                                    //		if (players.追加状态列表.ContainsKey(1008000095))
                                    //		{
                                    //			追加状态类 追加状态类 = players.追加状态列表[1008000095];
                                    //			num2 += 追加状态类.FLD_sj;
                                    //			追加状态类.时间结束事件();
                                    //		}
                                    //		追加状态类 追加状态类2 = new 追加状态类(players, num2, 1008000095, 1);
                                    //		players.追加状态列表.Add(追加状态类2.FLD_PID, 追加状态类2);
                                    //		players.FLD_人物_追加_经验百分比 += 1.1;
                                    //		players.状态效果(BitConverter.GetBytes(1008000095), 1, num2);
                                    //		players.更新武功和状态();
                                    //		players.保存人物的数据();
                                    //		players.保存人物数据存储过程();//如果有效果，给其他的也加上
                                    //		//players.系统提示("恭喜您获得势力战前50名奖励[1.2倍经验状态5小时]", 10, "奖励");

                                    //	}
                                    //	catch (Exception)
                                    //	{
                                    //	}
                                    //}
                                    players.更新武功和状态();

                                }
                            }
                            else if (i >= 10 && i < 20 && World.势力战十到二十名奖励武勋 > 0)
                            {
                                players.Player_WuXun += World.势力战十到二十名奖励武勋;
                                players.查百宝阁元宝数();
                                players.检察积分数据(20, 1);
                                players.保存元宝数据();
                                players.系统提示("恭喜您获得奖励" + World.势力战十到二十名奖励武勋 + "武勋,赠品元宝20个。", 10, "系統提示");
                                if (players != null)
                                    players.更新武功和状态();
                            }
                            else if (i >= 20 && i <= 100)
                            {
                                players.Player_WuXun += World.势力战二十到五十名奖励武勋;
                                players.查百宝阁元宝数();
                                players.检察积分数据(10, 1);
                                players.保存元宝数据();
                                players.系统提示("恭喜您获得奖励" + World.势力战二十到五十名奖励武勋 + "武勋,赠品元宝10个，1.2经验加成5小时", 10, "系統提示");
                                if (players != null)
                                    players.更新武功和状态();
                            }
                        }
                    }
                    dataTable.Dispose();
                }
            }
            catch
            {
            }
        }

        public void 时间结束事件4(object source, ElapsedEventArgs e)
        {
            try
            {
                World.势力战进程 = 5;
                //World.conn.发送("势力战进程|" + World.势力战进程);
                this.Dispose();
            }
            catch (Exception arg)
            {
                Form1.WriteLine(1, "势力战 时间结束事件4 出错：" + arg);
            }
        }

        public void 时间结束事件5(object source, ElapsedEventArgs e)
        {
            try
            {
                if (DateTime.Now.Hour == World.三转势力战开启小时)
                {
                    World.AddNpc(15220, 0f, 0f, 801);
                }
                else
                {
                    World.AddNpc(15136, 0f, 0f, 801);
                }
                this.时间5.Enabled = false;
                this.时间5.Close();
                this.时间5.Dispose();
            }
            catch (Exception arg)
            {
                Form1.WriteLine(1, "势力战 时间结束事件5 出错：" + arg);
            }
        }

        public void Dispose()
        {
            try
            {
                foreach (Players current in World.allConnectedChars.Values)
                {
                    if (current.人物坐标_地图 == 801)
                    {
                        current.势力战临时正邪值 = current.Player_Zx;
                        current.势力战杀人数 = 0;
                        current.势力战死亡数 = 0;
                        current.移动(-5f, -50f, 15f, 1201);
                        current.更新装备效果();
                        current.更新人物数据(current);
                        current.更新广播人物数据();
                    }
                }
                World.势力战进程 = 0;
                World.势力战时间 = 0;
                World.势力战正分数 = 0;
                World.势力战邪分数 = 0;
                World.几转势力战 = 0;
                //World.势力战正人数 = 0;
                //World.势力战邪人数 = 0;
                if (World.势力战掉线玩家.Count > 0)
                {
                    World.势力战掉线玩家.Clear();
                }
                if (this.时间1 != null)
                {
                    this.时间1.Enabled = false;
                    this.时间1.Close();
                    this.时间1.Dispose();
                    this.时间1 = null;
                }
                if (this.时间2 != null)
                {
                    this.时间2.Enabled = false;
                    this.时间2.Close();
                    this.时间2.Dispose();
                    this.时间2 = null;
                }
                if (this.时间3 != null)
                {
                    this.时间3.Enabled = false;
                    this.时间3.Close();
                    this.时间3.Dispose();
                    this.时间3 = null;
                }
                if (this.时间4 != null)
                {
                    this.时间4.Enabled = false;
                    this.时间4.Close();
                    this.时间4.Dispose();
                    this.时间4 = null;
                }
                if (this.时间5 != null)
                {
                    this.时间5.Enabled = false;
                    this.时间5.Close();
                    this.时间5.Dispose();
                    this.时间5 = null;
                }
                if (World.eve != null)
                {
                    World.eve = null;
                }
            }
            catch
            {
            }
            finally
            {
              //  World.conn.发送("势力战进程|" + World.势力战进程);
                //World.conn.发送(string.Concat(new object[]
                //{
                //    "势力战人数|",
                //    World.势力战正人数,
                //    "|",
                //    World.势力战邪人数
                //}));
                World.势力战进程 = 0;
                World.势力战时间 = 0;
                World.势力战正分数 = 0;
                World.势力战邪分数 = 0;
                //World.势力战正人数 = 0;
                //World.势力战邪人数 = 0;
                if (World.势力战掉线玩家.Count > 0)
                {
                    World.势力战掉线玩家.Clear();
                }
                if (this.时间1 != null)
                {
                    this.时间1.Enabled = false;
                    this.时间1.Close();
                    this.时间1.Dispose();
                    this.时间1 = null;
                }
                if (this.时间2 != null)
                {
                    this.时间2.Enabled = false;
                    this.时间2.Close();
                    this.时间2.Dispose();
                    this.时间2 = null;
                }
                if (this.时间3 != null)
                {
                    this.时间3.Enabled = false;
                    this.时间3.Close();
                    this.时间3.Dispose();
                    this.时间3 = null;
                }
                if (this.时间4 != null)
                {
                    this.时间4.Enabled = false;
                    this.时间4.Close();
                    this.时间4.Dispose();
                    this.时间4 = null;
                }
                if (this.时间5 != null)
                {
                    this.时间5.Enabled = false;
                    this.时间5.Close();
                    this.时间5.Dispose();
                    this.时间5 = null;
                }
                if (World.eve != null)
                {
                    World.eve = null;
                }
            }
        }
    }
}
